﻿Imports System.Data.OleDb
Public Class frmLoaiSanPham
    Private Sub frmLoaiSanPham_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoaiSanPham_Load()
        'Phân quyền Form
        Dim aPermission(4) As Boolean
        aPermission = PhanQuyenForm("MenuLoaiSanPham", frmMain.UserId)
        cmdAddNew.Enabled = aPermission(0)
        cmdEdit.Enabled = aPermission(1)
        cmdDelete.Enabled = aPermission(2)
    End Sub
    Public Sub LoaiSanPham_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsLoaiSanPham As New DataSet
            Dim dtLoaiSanPham As New DataTable
            Dim query As String = "Select * From LoaiSanPham Order By TenLoai ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsLoaiSanPham)
            dbConn.Close()
            dtLoaiSanPham = dsLoaiSanPham.Tables(0)
            dtLoaiSanPham.Columns.Add("STT")
            For i As Integer = 0 To dtLoaiSanPham.Rows.Count - 1
                dtLoaiSanPham.Rows(i)("STT") = i + 1
            Next
            dgvLoaiSanPham.AutoGenerateColumns = False
            dgvLoaiSanPham.DataSource = dtLoaiSanPham
            'set focus to first row
            If dgvLoaiSanPham.Rows.Count - 1 > 0 Then
                dgvLoaiSanPham.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub


    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            ShowModalForm(frmThemLoaiSanPham, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            frmThemLoaiSanPham.IDLoai = Integer.Parse(dgvLoaiSanPham.Rows(dgvLoaiSanPham.CurrentCell.RowIndex).Cells("IDLoai").Value.ToString)
            ShowModalForm(frmThemLoaiSanPham, False, False)
            frmThemLoaiSanPham.IDLoai = -1
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim IDLoai As Integer = Integer.Parse(dgvLoaiSanPham.Rows(dgvLoaiSanPham.CurrentCell.RowIndex).Cells("IDLoai").Value.ToString)
            Dim LoaiSanPham As String = dgvLoaiSanPham.Rows(dgvLoaiSanPham.CurrentCell.RowIndex).Cells("TenLoai").Value.ToString
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa loại sản phầm : " & LoaiSanPham, _
                  "Xóa loại sản phẩm", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                'Update
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Delete from LoaiSanPham Where IDLoai=" & IDLoai
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                LoaiSanPham_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsLoaiSanPham As New DataSet
            Dim dtLoaiSanPham As New DataTable
            Dim query As String = "Select * From LoaiSanPham Where TenLoai Like N'%" & txtTextSearch.Text & "%' Order By TenLoai ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsLoaiSanPham)
            dbConn.Close()
            dtLoaiSanPham = dsLoaiSanPham.Tables(0)
            dtLoaiSanPham.Columns.Add("STT")
            For i As Integer = 0 To dtLoaiSanPham.Rows.Count - 1
                dtLoaiSanPham.Rows(i)("STT") = i + 1
            Next
            dgvLoaiSanPham.AutoGenerateColumns = False
            dgvLoaiSanPham.DataSource = dtLoaiSanPham
            'set focus to first row
            If dgvLoaiSanPham.Rows.Count - 1 > 0 Then
                dgvLoaiSanPham.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class